// pages/exam/examResult/examResult.js
var app = getApp();
var config = require('../../../config.js');
var util = require('../../../utils/util.js');

// 考试结果页面分2种情况 1考试结束进入  2查询成绩进入 3分享链接进入
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fromType: -1,
    exam_single_point: 0,
    exam_mult_point: 0,
    exam_boolean_point: 0,
    exam_input_point: 0,
    exam_total_point: 0,
    exam_unsureness_point: 0,
    buttonClicked: false,

    title: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var fromType = options.fromType;

    this.setData({
      fromType: fromType,

    });
    var mainId = options.mainId;
    if (mainId) {
      that.setData({
        mainId: mainId

      });
    }
    //准考证号查询
    var examNum = options.examNum;
    if (examNum) {
      that.setData({
        examNum: examNum,

      });
    }
    //考试id
    var examId = options.examId;
    if (examId) {
      that.setData({
        examId: examId,

      });
    }
    // console.log("result--mainId-----"+mainId);
    //   console.log("result--examId-----" + examId);
    //   console.log("result--examNum-----" + examNum);

    /***** 存入的key相同 实体字段不一样  */
    if (fromType == 1) {
      this.initData();
      that.setData({
        fromType: fromType,
        userFace: app.globalData.userInfo.avatarUrl,
        userName: app.globalData.userInfo.nickName,
      });
    } else if (fromType == 2) {
      //通过查询成绩进入
      this.initDataV2();
    } else if (fromType == 3) {
      //分享进入页面
      this.checkResult();
    }

  },


  initData: function () {
    var that = this;
    try {
      const value = wx.getStorageSync(config.examResultInfo)
      if (value) {
        console.log('ressults value====' + JSON.stringify(value))
        //   //考试时间转化
        var examDescInfo = value.examInfo;
        if (examDescInfo) {
          // var exam_startTime = util.timeStamp2StringV2(examDescInfo.startTime);
          // var exam_endTime = util.timeStamp2StringV2(examDescInfo.endTime);

          // that.setData({
          //   exam_startTime: exam_startTime,
          //   exam_endTime: exam_endTime

          // });


          var st = examDescInfo.startTime;
          var et = examDescInfo.endTime;
          // var st = s.substring(0, s.indexOf(" "));
          // var et = e.substring(0, e.indexOf(" "));
          var title = examDescInfo.theme;
          that.setData({
            exam_startTime: st,
            exam_endTime: et,
            title: title
          });
        }

        that.setData({
          //分值
          exam_single_value: value.exam_singleTotal,
          exam_mult_value: value.exam_multTotal,
          exam_boolean_value: value.exam_booleanTotal,
          exam_input_value: value.exam_inputTotal,
          exam_unsureness_value: value.exam_unsurenessTotal,//不定项

          exam_total_value: value.exam_ScoreTotal,

          //得分
          exam_single_point: value.exam_singleScore,
          exam_mult_point: value.exam_multScore,
          exam_boolean_point: value.exam_booleanScore,
          exam_input_point: value.exam_inputScore,
          exam_unsureness_point: value.exam_unsurenessScore,//不定项

          exam_total_point: value.exam_Score,

          examId: value.examId

        });
      }
    } catch (e) {
      // Do something when catch error
    }


  },

  initDataV2: function () {
    var that = this;
    try {
      const value = wx.getStorageSync(config.examResultInfo)
      // console.log(value)
      console.log('value===' + JSON.stringify(value))
      if (value) {
        //   //考试时间转化
        var examDescInfo = value.xzExam;
        if (examDescInfo) {
          // var exam_startTime = util.timeStamp2StringV2(examDescInfo.startTime);
          // var exam_endTime = util.timeStamp2StringV2(examDescInfo.endTime);

          var st = examDescInfo.startTime;
          var et = examDescInfo.endTime;
          // var st = s.substring(0,s.indexOf(" "));
          // var et = e.substring(0, e.indexOf(" "));
          that.setData({
            exam_startTime: st,
            exam_endTime: et

          });
        }

        //设置用户信息
        var userInfo = value.user;
        if (userInfo) {
          that.setData({
            userFace: userInfo.avatarUrl,
            userName: userInfo.nickname,

          });
        }

        that.setData({
          //总分
          exam_single_value: value.singlePoint,
          exam_mult_value: value.mulitplePoint,
          exam_boolean_value: value.checkPoint,
          exam_input_value: value.completionPoint,
          exam_unsureness_value: value.unsurenessPoint,//不定项

          exam_total_value: value.totalPoint

        });

        if (value.xzExamRecord) {
          that.setData({
            //个人得分
            exam_single_point: value.xzExamRecord.singlePoint,
            exam_mult_point: value.xzExamRecord.multiplePoint,
            exam_boolean_point: value.xzExamRecord.checkPoint,
            exam_input_point: value.xzExamRecord.completionPoint,
            exam_unsureness_point: value.xzExamRecord.unsurenessPoint,//不定项

            exam_total_point: value.xzExamRecord.totalPoint,
            examId: value.xzExamRecord.examId
          });


        }
      }
    } catch (e) {
      // Do something when catch error
    }


  },

  //查询分享者给的成绩
  checkResult: function () {
    var that = this;
    console.log(that.data.mainId);
    console.log(that.data.examId);
    console.log(that.data.examNum);

    wx.request({
      url: config.queryExamResult,
      method: "POST",
      data: {
        openId: that.data.mainId,
        examId: that.data.examId,
        examNum: that.data.examNum
      },

      success: function (ret) {
        var data = ret.data
        if (data.success == 0) {
          var examInfo = data.rows;
          console.log(examInfo)
          if (examInfo) {
            //为了方便渲染数据 暂时先用这种方式处理
            try {
              wx.setStorageSync(config.examResultInfo, examInfo)
              //刷新页面
              that.initDataV2();
            } catch (e) {

            }


          } else {
            wx.showToast({
              title: '暂无成绩',
              icon: "none"

            })
          }

        } else {
          wx.showToast({
            title: '暂无成绩',
            icon: "none"

          })
        }
      }
    })

  },








  /**
   * openId,examId,single_point,multiple_point,check_point,completion_point
      examNum
   */
  //提交成绩
  submitExamResult: function () {
    var that = this;
    util.showLoading('提交中...');
    util.buttonClicked(this);
    console.log(app.globalData.openid)
    if (util.isEndActivityByTime(that.data.exam_endTime)) {
      wx.showModal({
        title: '提示',
        content: '时间到，您未在规定时间内完成考试！本次考试没有成绩！',
        success(res) {
          util.hideLoading()
          if (res.confirm) {
            //直接返回到首页
            wx.navigateBack({
              delta: 100
            })
          } else if (res.cancel) {
          }
        }
      })

      return;
    }
    wx.request({
      url: config.addExamScore,
      method: "POST",
      data: {
        examId: that.data.examId,
        openId: app.globalData.openid,
        single_point: that.data.exam_single_point,
        multiple_point: that.data.exam_mult_point,
        check_point: that.data.exam_boolean_point,
        completion_point: that.data.exam_input_point,
        unsureness_point: that.data.exam_unsureness_point,//不定项
      },
      success: function (ret) {
        util.hideLoading()
        var data = ret.data
        if (data.success == 0) {
          wx.showToast({
            title: '提交成绩成功',
            icon: 'none',
            duration: 2000
          })

          setTimeout(function () {
            //提交成绩成功 返回首页  delta: 值很大时 直接回到首页
            wx.navigateBack({
              delta: 100
            })

          }, 2000);


        } else {
          util.hideLoading()
          setTimeout(function () {
            wx.showToast({
              title: '提交失败',
            })
          }, 2000);

        }
      }
    })

  },

  /**
   *  path: '/pages/exam/examResult/examResult?pageId=' + config.gameInstruction +
        '&mainId=' + app.globalData.openid,
   */
  onShareAppMessage(res) {
    var that = this;
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    console.log('/pages/index/index?pageId=' + config.examResult + '&mainId=' + that.data.mainId +
      '&examNum=' + that.data.examNum + '&examId=' + that.data.examId);
    return {
      title: '学法风暴新版来袭，更多精彩进入体验',
      path: '/pages/index/index?pageId=' + config.examResult + '&mainId=' + that.data.mainId +
        '&examNum=' + that.data.examNum + '&examId=' + that.data.examId,
      success: function (res) {
        console.log("xiaohuanudfas" + JSON.stringify(res))
      }
    }
  },


  onceAgain: function () {
    if (this.data.fromType == 1) {
      // 提示用户 重新答题本次成绩将清零
      wx.showModal({
        title: '提示',
        content: '重新答题本次成绩将清零',
        success(res) {
          if (res.confirm) {
            console.log('用户点击确定')
            // wx.navigateBack({
            //   delta: 1
            // })

            wx.redirectTo({
              url: '../examGoing/examGoing'
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })


    } else {
      wx.navigateBack({
        delta: 1
      })
    }


  }

})